﻿using _1._2._2015_MVCTodo.Models;
using System;
using System.Collections.Generic; 
using System.Linq;
using System.Web;

namespace _1._2._2015_MVCTodo
{
    public class TodoService
    {
        #region Private Class Static Variables

        private static TodoService _instance = null;
        public static TodoService Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new TodoService();
                }
                return _instance;
            }
        }

        #endregion

        #region Private Class Instance Variables

        private List<TodoViewModel> _todos;
        private int _seedId;

        #endregion

        #region Constructors
        private TodoService()
        {
            _seedId = 1;
            _todos = new List<TodoViewModel>();
        }

        #endregion

        #region Class Static Methods
        //public static string GetTodoVersion()
        //{
        //    return "1.0";
        //}

        //public static TodoService GetTodoServiceInstance()
        //{
        //    return new TodoService();
        //}

        #endregion

        #region Class Instance Methods
        public List<TodoViewModel> GetTodos()
        {
            return _todos;
        }

        public TodoViewModel GetTodo(int id)
        {
            return _todos.FirstOrDefault(t => t.Id == id);
        }

        public int CreateTodo(string text)
        {
            TodoViewModel model = new TodoViewModel
            {
                DateCreated = DateTime.Now,
                Text = text,
                Id = _seedId
            };

            _todos.Add(model);
            _seedId++;

            return model.Id;
        }

        public void UpdateTodo(int id, string text, bool isCompleted)
        {
            TodoViewModel model = _todos.FirstOrDefault(t => t.Id == id);

            if (model == null)
                return;

            model.Text = text;
            model.IsCompleted = isCompleted;
            if (isCompleted)
            {
                model.DateCompleted = DateTime.Now;
            }
        }

        #endregion
    }
}